﻿using System;
using System.Web.UI.WebControls;
using Team4Mock.Entity;
using System.Data;

namespace Team4Mock.UserControl.Programme
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        ProgrammeEntity pe=new ProgrammeEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Rediect();", true);
                    }
                }
                else
                {
                    
                    btnDel.Visible = false;
                }

                if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["ID"])))
                {
                    try
                    {
                        pnView.Visible = false;
                        pnct.Visible = true;
                        btnDel.Visible = true;
                        int id = int.Parse(Request.QueryString["ID"]);
                        pe.ProgramId = id;
                        DataTable dt = new DataTable();
                        dt = pe.GetById(pe);
                        txtDes.Text = dt.Rows[0][2].ToString();
                        txtProName.Text = dt.Rows[0][1].ToString();
                        idContact.Value = dt.Rows[0][3].ToString();
                        if (dt.Rows[0][4].ToString() == "Yes")
                        {
                            ckisactive.Checked = true;
                        }
                        else
                        {
                            ckisactive.Checked = false;
                        }
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
                else
                {
                    btnDel.Visible = false;
                    pnView.Visible = true;
                    pnct.Visible = false;
                    LoadGV();
                }
            }

        }
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            pnView.Visible = false;
            pnct.Visible = true;
        }
        void LoadGV()
        {
            gvProgramme.DataSource = pe.GetDataTrue();
            gvProgramme.DataBind();
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Programme.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Amend
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                if (txtProName.Text=="")
                {
                    tb.Text = "Input ProgramName";
                }
                else
                {
                    try
                    {
                        int id = int.Parse(Request.QueryString["ID"]);
                        pe.ProgramId = id;
                        pe.ProgramName = txtProName.Text;
                        pe.Contact = int.Parse(idContact.Value);
                        pe.Description = txtDes.Text;
                        pe.IsActive = ckisactive.Checked ? 1 : 0;
                        pe.UpdateData(pe);
                        tb.Text = "Update Success";
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
            //Add
            else
            {
                if (txtProName.Text == "")
                {
                    tb.Text = "Input ProgramName";
                }
                else
                {
                    if (idContact.Value == "")
                    {
                        tb.Text = "Select Contact";
                    }
                    else
                    {
                        try
                        {
                            pe.ProgramName = txtProName.Text;
                            pe.Contact = int.Parse(idContact.Value);
                            pe.Description = txtDes.Text;
                            pe.IsActive = ckisactive.Checked ? 1 : 0;
                            pe.InsertData(pe);
                            tb.Text = "Insert Success";
                        }
                        catch
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                        }
                    }
                }
            }
        }

        private DataTable dt;
        protected void All_Click(object sender, EventArgs e)
        {
            LoadGV();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "ABCDE";
            pe.KeyWord = "ABCDE";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "FGHIJ";
            pe.KeyWord = "FGHIJ";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "KMLN";
            pe.KeyWord = "KMLN";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "OPQR";
            pe.KeyWord = "OPQR";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "STUV";
            pe.KeyWord = "STUV";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            ViewState["sortkey"] = "WXYZ";
            pe.KeyWord = "WXYZ";
            dt = pe.Search(pe);
            if (dt.Rows.Count > 0)
            {
                gvProgramme.DataSource = dt;
                gvProgramme.DataBind();

                mess.Text = "";
            }
            else
            {
                ViewState.Remove("sortkey");
                mess.Text = "No Record";
            }
        }

        protected void ckactive_CheckedChanged(object sender, EventArgs e)
        {
            if (ViewState["sortkey"] != null)
            {
                string sort = ViewState["sortkey"].ToString();
                pe.KeyWord = sort;
                if (ckactive.Checked == true)
                {
                    gvProgramme.DataSource = pe.Search(pe);
                    gvProgramme.DataBind();
                }
                else
                {
                    gvProgramme.DataSource = pe.SearchTrue(pe);
                    gvProgramme.DataBind();
                }
            }
            else
            {
                if (ckactive.Checked == true)
                {
                    gvProgramme.DataSource = pe.GetData();
                    gvProgramme.DataBind();
                }
                else
                {

                    gvProgramme.DataSource = pe.GetDataTrue();
                    gvProgramme.DataBind();
                }
            }
            
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                pe.ProgramId = id;
                pe.Del(pe);
                Response.Redirect("/FormView/Programme.aspx");
            }
            catch
            {
                tb.Text = "Error while being processed";
            }
        }
        public SortDirection SortDirection
        {
            get
            {
                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void gvProgramme_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
        {
            DataTable dt = new DataTable();
            if (ViewState["sortkey"] != null)
            {
                string sortkey = ViewState["sortkey"].ToString();
                pe.KeyWord = sortkey;
                if (ckactive.Checked == true)
                {
                    dt = pe.Search(pe);
                }
                else
                {
                    dt = pe.SearchTrue(pe);
                }
            }
            else
            {
                dt = ckactive.Checked ? pe.GetData() : pe.GetDataTrue();
            }

            string sortExpression = e.SortExpression;
            string direction = string.Empty;
            string sort = "";
            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                direction = " DESC";
                sort = "↑";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                direction = " ASC";
                sort = "↓";
            }
            if (sortExpression == "Name")
            {
                gvProgramme.Columns[1].HeaderText = "Programme Name " + sort;
                gvProgramme.Columns[2].HeaderText = "Description";
                gvProgramme.Columns[3].HeaderText = "Contact";
            }
            if (sortExpression == "Description")
            {
                gvProgramme.Columns[1].HeaderText = "Programme Name";
                gvProgramme.Columns[2].HeaderText = "Description " + sort;
                gvProgramme.Columns[3].HeaderText = "Contact";
            }
            
            if (sortExpression == "Contact")
            {
                gvProgramme.Columns[1].HeaderText = "Programme Name";
                gvProgramme.Columns[2].HeaderText = "Description";
                gvProgramme.Columns[3].HeaderText = "Contact " + sort;
            }
            dt.DefaultView.Sort = sortExpression + direction;

            gvProgramme.DataSource = dt;
            gvProgramme.DataBind();
        }
    }
}